/*
 * Copyright 2010 Michael Nitsche
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.mnit.basis.swt.dnd.transfer;

import de.mnit.basis.fehler.Fehler;

/**
 * @author Michael Nitsche
 * 14.06.2010	Erstellt
 */
public class IDObjekt {

	public final Class<?> klasse;
	public final int id;

	public IDObjekt(Class<?> klasse, int id) {
		this.klasse=klasse;
		this.id=id;
	}


	public static Object zuByteArray(IDObjekt[] ido) {
		StringBuilder sb=new StringBuilder();
		for(int i=0; i<ido.length; i++) {
			String s=(i==0 ? "" : ",")+ido[i].toString();
			sb.append(s);
		}
		return sb.toString().getBytes();
	}

	public static IDObjekt[] zuObjekt(byte[] ba) {
		String s=new String(ba);
		String[] sa=s.split(",");
		IDObjekt[] erg=new IDObjekt[sa.length];
		for(int i=0; i<sa.length; i++)
			erg[i]=zuObjekt(sa[i]);
		return erg;
	}

	private static IDObjekt zuObjekt(String s) {
		String[] sa=s.split(":");
		String klassenname=sa[0];
		int id=Integer.parseInt(sa[1]);
		Class<?> klasse;
		try {
			klasse=Class.forName(klassenname);
		}
		catch(ClassNotFoundException e) {
			throw Fehler.sonstige.da_Untypisch(e, s, klassenname);
		}
		return new IDObjekt(klasse, id);
	}

	public String toString() {
		return klasse.getName()+":"+id;
	}

}
